<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class VersionController extends CommonController {

    protected $_modelName = "Version";

    protected function setCode() {
        $this->_code = "System"; // 属于系统管理模块
    }

    protected function _getQueryArr(&$where, &$order) {
        $where = array();

        if (strlen(trim(I("sDevice"))) > 0 && trim(I("sDevice")) != 'all') {
            $where ['device'] = trim(I("sDevice"));
        }
        if (strlen(trim(I("sVersion"))) > 0) {
            $where ['version'] = trim(I("sVersion"));
        }
        if (strlen(trim(I("sStatus"))) > 0 && intval(I("sStatus", -1)) >= 0) {
            $where ['status'] = trim(I("sStatus"));
        }
        $where['isOn'] = 1;

        $orderBy = 'verID';
        $desc = 'desc';

        $order = array(
            $orderBy => $desc
        );
    }

    /**
     * 查看列表
     */
    public function index() {
        $this->checkPriv('ver', 1, false);

        $this->_getOnePageData("getVersionList", "getVersionListLength");
    }

    public function saveVersion() {
        $id = intval(I("post.verID"));

        if ($id > 0) {
            $this->_updateVersion();
        } else {
            $this->_addVersion();
        }
    }

    /**
     *  添加
     */
    public function _addVersion() {
        $_POST['appKey'] = get_rand_str(43);
//        $_POST['appID'] = time();
        $this->_ajaxOperate('addVersion', 'ver', 2);
    }

    /**
     * 更新基本信息(需要更新什么就提交什么)
     */
    public function _updateVersion() {
        
        $verID = I("verID", 0);
        
        if ($verID > 0) {
            $verArr = D("Version")->getVersion($verID);
            if($verArr) {
                $data['device'] = trim(I("device"));
                $data['version'] = I("version");
                $data['status'] = I("status", 0);
                $data['url'] = I("url");
                $data['verID'] = $verID;
                
                if($verArr['isOn'] == 1) {
//                    $data['appID'] = $verArr['appID'];
//                    $data['appKey'] = $verArr['appKey'];
                    $this->_ajaxOperate('updateVersion', 'ver', 2, $data);
                } else {
                    $this->_error = '该版本已删除';
                }
            } else {
                $this->_error = '数据不存在';
            }
        } else {
            $this->_error = '无效版本ID';
        }
        $this->ajaxReturn(array(
            'result' => false,
            'desc' => $this->_error
                ), 'json');
    }

    public function updateStatus() {
        $verID = I("verID", 0);
        
        if ($verID > 0) {
            $verArr = D("Version")->getVersion($verID);
            if($verArr) {
                $data['status'] = I("status", 0);
                $data['verID'] = $verID;
                if($verArr['isOn'] == 1) {
                    $data['appID'] = $verArr['appID'];
                    $data['appKey'] = $verArr['appKey'];
                    $this->_ajaxOperate('updateVersion', 'ver', 2, $data);
                } else {
                    $this->_error = '该版本已删除';
                }
            } else {
                $this->_error = '数据不存在';
            }
        } else {
            $this->_error = '无效版本ID';
        }
        $this->ajaxReturn(array(
            'result' => false,
            'desc' => $this->_error
                ), 'json');
    }

    /**
     * 删除
     */
    public function deleteVersion() {
        $verID = I("verID", 0);
        
        if ($verID > 0) {
            $this->_ajaxOperate('deleteVersion', 'ver', 2, $verID);
        } else {
            $this->_error = '无效版本ID';
        }
        $this->ajaxReturn(array(
            'result' => false,
            'desc' => $this->_error
                ), 'json');
        
    }

}
